package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.bocai.yuweiwangluo.activity.HomeActivity;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.RegistBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IRegistView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class RegistPresenter {
    private RegistBean registBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IRegistView registView;
    private Context context;
    private BaseBean bean;
    private int requsttype;
    private int delaytime;
    private Button getCodebtn;
    private BaseView baseView;
    private MyOkHttpClient myOkHttpClient;
    public RegistPresenter(Context context, IRegistView registView,Button getCodebtn,BaseView baseView)
    {
        myOkHttpClient = new MyOkHttpClient(context,baseView);
        this.context = context;
        this.registView = registView;
        this.getCodebtn=getCodebtn;
        this.baseView=baseView;
    }

    /**
     * 验证码验证
     */
    public void verificationCode()
    {
        requsttype=2;
        if("".equals(registView.getNick()))
        {
            Toast.makeText(context,"请输入昵称",Toast.LENGTH_SHORT).show();
        }
        else if("".equals(registView.getTel()))
        {
            Toast.makeText(context,"请输入手机号",Toast.LENGTH_SHORT).show();
        }
        else if (registView.getTel().length()!= 11)
        {
            Toast.makeText(context, "手机号错误", Toast.LENGTH_SHORT).show();
        }
        else if ("".equals(registView.getCode()))
        {
            Toast.makeText(context, "请输入验证码", Toast.LENGTH_SHORT).show();
        }
        else if ("".equals(registView.getPwd()))
        {
            Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
        }
        else if (registView.getPwd().length()<6)
        {
            Toast.makeText(context, "您设置的密码不足6位", Toast.LENGTH_SHORT).show();
        }
        else if("".equals(registView.getPwdtrue()))
        {
            Toast.makeText(context, "请确认密码", Toast.LENGTH_SHORT).show();
        }
        else if (!registView.getPwd().equals(registView.getPwdtrue()))
        {
            Toast.makeText(context, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
        }
        else if(!registView.isChecked())
        {
            Toast.makeText(context, "请同意该协议", Toast.LENGTH_SHORT).show();
        }
        else
        {
            baseView.showLoading();
            myOkHttpClient.verificationCode(UrlData.BASEURL + UrlData.VERIFICATIONCODE, registView.getTel(), registView.getCode(),handler);
        }
    }
    public void getCode()
    {
        if ("".equals(registView.getTel()))
        {
            Toast.makeText(context,"请输入手机号",Toast.LENGTH_SHORT).show();
        }
        if (registView.getTel().length() != 11)
        {
            Toast.makeText(context, "手机号错误", Toast.LENGTH_SHORT).show();
        } else
        {
            requsttype = 1;
            myOkHttpClient.getCode(UrlData.BASEURL + UrlData.GETCODE, registView.getTel(), handler);
        }
    }
    /**
     * 注册请求
     */
    public void regist(){
        requsttype = 3;
        myOkHttpClient.register(UrlData.BASEURL + UrlData.REGISTER, registView.getTel(), registView.getPwd(), registView.getNick(), registView.getInvitation(), handler);
    }


    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            switch (msg.what)
            {
                case RESPONSE_SUCCEED:
                    Log.e("tag",result);
                    if (requsttype == 1)//发送验证码
                    {
                        bean = gson.fromJson(result, BaseBean.class);
                        if (0==bean.getState())
                        {
                            try
                            {
                                Toast.makeText(context,"验证码已发送至手机",Toast.LENGTH_SHORT).show();
                                delaytime = 60;
                                getCodebtn.setClickable(false);
                                new Thread(new ThreadShow()).start();
                            } catch (Exception e)
                            {
                            }
                        } else
                        {
                            Toast.makeText(context, bean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }else if(requsttype == 2)//验证验证码
                    {
                        bean = gson.fromJson(result, BaseBean.class);
                        if (0==bean.getState())
                        {
                            regist();
                        } else
                        {
                            Toast.makeText(context, bean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    } else if (requsttype == 3)
                    {//注册
                        baseView.hideLoading();
                        registBean=gson.fromJson(result,RegistBean.class);
                        if (0==bean.getState())
                        {
                            MyConst.user_id=registBean.getRow().getId();
                            SharedPreferences preferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
                            SharedPreferences.Editor edit =preferences.edit();
                            edit.putString("user_name", registView.getTel());
                            edit.putString("password",registView.getPwd());
                            edit.putString("user_id",registBean.getRow().getId());
                            edit.putString("nickname",registBean.getRow().getNickname());
                            edit.putString("number",registBean.getRow().getNumber());
                            edit.putString("isremember", "false");
                            edit.commit();
                            context.startActivity(new Intent(context, HomeActivity.class));
                            registView.finishActivity();
                            Toast.makeText(context, "注册成功",Toast.LENGTH_SHORT).show();
                        } else
                        {
                            Toast.makeText(context, bean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
                case RESPONSE_FAILED:
                    baseView.hideLoading();
                    baseView.showError(result);
                    break;
                case 88:
                    getCodebtn.setText(delaytime + "s");
                    if (delaytime == 0)
                    {
                        getCodebtn.setClickable(true);
                        getCodebtn.setText("再次获取");
                    }
                    break;
            }
        }
    };
    // 线程类
    class ThreadShow implements Runnable
    {
        @Override
        public void run()
        {
            while (delaytime > 0)
            {
                try
                {
                    Thread.sleep(1000);
                    delaytime--;
                    Message msg = new Message();
                    msg.what = 88;
                    handler.sendMessage(msg);
                } catch (Exception e)
                {
                }
            }
        }
    }
}